<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" type="text/css" media="screen" href="/layui/css/layui.css">
    <script src="/js/libs/jquery-2.1.1.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/bootstrap-select.min.css">
    <script src="/js/bootstrap/bootstrap.min.js"></script>
    <script src="/js/bootstrap/bootstrap-select.min.js"></script>
    <script src="/js/jq.js"></script>
    <script src="/js/plugin/bootstrapvalidator/bootstrapValidator.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/layui/layui.js"></script>
    <script src="/js/dict.js"></script>
    <script src="/js/libs/jquery.ztree.all-3.5.min.js"></script>
    <script src="/js/my/ztree-menu.js"></script>
    <script src="/js/my/permission.js"></script>
    <script src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
    <script src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
</head>
<body>
<div>
    <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
        <div>
            <header style="height: 100%">
                <div align="left">
                    <table style="width: 100%">
                        <tr>
                            <td>
                                <form id="form" class="form-inline" method="post" action="/stock/export"
                                      enctype="multipart/form-data">
                                    <!--<form class="form-inline" action="" onsubmit="return false" id="form">-->
                                    <div class="form-group">
                                        <input id="type" name="type" type="hidden" class="form-control" value="person">
                                        商品编码:
                                        <input id="goodsCode" name="goodsCode" type="text" class="form-control"
                                               placeholder="商品编码">
                                        商品名称:
                                        <input id="goodsName" name="goodsName" type="text" class="form-control"
                                               placeholder="商品名称">
                                        分类:
                                        <input class='form-control' type='hidden' name='category' id='category'>
                                        <input class='form-control' placeholder='商品类型' type='text' name='categoryName'
                                               id='categoryName' readonly>
                                        <a href="#" class="layui-btn layui-btn-sm"
                                           onclick="showTree('category', 'categoryName', 'goodsCategory', '商品类型', 'root', 'select');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        所属公司:
                                        <select class="form-control" name="companyId" id="companyId"
                                                title="所属公司" data-live-search="true">
                                            <option value="" selected>--请选择--</option>
                                        </select>
                                    </div>
                                    <br><br>
                                    <div class="form-group">
                                        所属部门:
                                        <input class='form-control' type='hidden' name='deptId' id='deptId'>
                                        <input class='form-control' placeholder='所属部门' type='text' name='deptName'
                                               id='deptName' readonly>
                                        <a href="#" class="layui-btn layui-btn-sm"
                                           onclick="showSpecialTree('deptId', 'deptName', 'dept', '部门', 'root', 'select','fasDeptTree');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                        所属人员:
                                        <input class='form-control' type='hidden' name='userId' id='userId'>
                                        <input class='form-control' placeholder='人员' type='text'
                                               name='userName' id='userName'
                                               data-bv-notempty='true' readonly>
                                        <a href="#" class="layui-btn layui-btn-sm"
                                           onclick="showTableAndFlushFields('user','user', '用户', true, false, 'userId:id', 'userName:nickname');return false;">
                                            <i class="layui-icon">&#xe615;</i>
                                        </a>
                                    </div>
                                    <br><br>
                                </form>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <button id="searchBt" class="layui-btn layui-btn-sm search">
                                    <i class="layui-icon">&#xe615;</i>搜索
                                </button>
                            </td>
                            <td align="right">
                                <iframe id="export" style="display:none"></iframe>
                                <button class='layui-btn layui-btn-sm' permission="fas:stockP:export"
                                        onclick="exportData()">
                                    <i class='layui-icon'>&#xe601;</i> 导出
                                </button>
                                <button class="layui-btn layui-btn-sm" permission="fas:stock:importData" id="import">
                                    <i class="layui-icon">&#xe67c;</i> 导入
                                </button>
                                <button class="layui-btn layui-btn-sm" onclick="openIFrame('addStock.html?type=p','添加库存')" permission="fas:stockP:add">
                                    <i class="layui-icon">&#xe608;</i> 添加
                                </button>
                            </td>
                        </tr>
                    </table>
                </div>
            </header>

            <div>
                <div class="widget-body no-padding">
                    <table id="dt-table" class="table table-striped table-bordered table-hover" style="width:100%">
                        <thead>
                        <tr>
                        </tr>
                        <tr>
                            <th>id</th>
                            <th>商品编码</th>
                            <th>商品名称</th>
                            <th>商品分类</th>
                            <th>所属人员</th>
                            <th>所属部门</th>
                            <th>所属公司</th>
                            <th>总数量</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

<script type="text/javascript" src="/js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="/js/jq.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/js/plugin/datatables/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="/layui/layui.js"></script>
<script type="text/javascript" src="/js/my/permission.js"></script>
<script type="text/javascript">
    var pers = checkPermission();
    $(function () {
        showDictSelect('companyId', 'company', '--请选择--');
    })

    var example;

    function init() {
        example =
            $('#dt-table').DataTable({
                "searching": false,
                "processing": false,
                "serverSide": true,
                "language": {
                    "url": "/js/plugin/datatables/Chinese.lang"
                },
                "ajax": {
                    "url": "/stock/list",
                    "type": "get",
                    "data": function (d) {
                        d.goodsCode = $('#goodsCode').val();
                        d.goodsName = $('#goodsName').val();
                        d.category = $('#category').val();
                        d.userId = $('#userId').val();
                        d.deptId = $('#deptId').val();
                        d.companyId = $('#companyId').val();
                        d.type = 'person';
                    }
                },
                "dom": "<'dt-toolbar'r>t<'dt-toolbar-footer'<'col-sm-10 col-xs-12 hidden-xs'i><'col-xs-12 col-sm-10' p v>>",
                "columns": [
                    {"data": "id", "defaultContent": "", "visible": false},
                    {"data": "goodsCode", "defaultContent": ""},
                    {"data": "goodsName", "defaultContent": ""},
                    {"data": "categoryName", "defaultContent": ""},
                    {"data": "userName", "defaultContent": ""},
                    {"data": "deptName", "defaultContent": ""},
                    {"data": "companyName", "defaultContent": ""},
                    {"data": "totalNumber1", "defaultContent": ""},
                    {
                        "data": "",
                        "defaultContent": "",
                        "orderable": false,
                        "render": function (data, type, row) {
                            var id = row['id'];
                            var href = "updateStock.html?id=" + id;
                            //var xgxz = buttonOther(row,"xgxz", "修改闲置数量", "", pers);
                            var edit = buttonEdit(href, "fas:stockP:edit", pers);
                            var del = buttonDel(id, "fas:stockP:del", pers);
                            return edit + del;
                        }
                    },
                ],
                "order": [[0, "asc"]],
                "createdRow": function (row, data, index) {
                    $(row).bind({
                        click: function () {
                            if ($(this).hasClass('success')) {
                                $(this).removeClass('success');
                            } else {
                                example.$('tr.success').removeClass('success');
                                $(this).addClass('success');
                            }
                        },
                        dblclick: function () {
                            if ($.inArray("fas:goods:edit", pers) >= 0) {
                                var id = data.id;
                                openIFrame("updateStock.html?id=" + id);
                            }
                        }
                    });
                }
            });
    }

    //修改闲置数量
    function buttonOther(obj, type, title, permission, pers) {
        if (permission != "") {
            if ($.inArray(permission, pers) < 0) {
                return "";
            }
        }
        // var btn = $("<button class='layui-btn layui-btn-xs' title='编辑' onclick='window.location=\"" + href +"\"'><i class='layui-icon'>&#xe642;</i></button>");
        var btn = $("<button class='layui-btn layui-btn-xs' title='" + title + "' onclick=\"approval('" + obj['id'] + "','" + obj['idleNumber'] + "','" + type + "')\">" + title + "</button>");

        return btn.prop("outerHTML");
    }

    function approval(id, num, type) {
        if ("xgxz" == type) {
            layer.open({
                id: "submit",
                type: 2,
                title: '修改闲置数量',
                skin: "layui-layer-molv",   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
                shadeClose: true,
                shade: 0.3,
                maxmin: true, //开启最大化最小化按钮
                success: function (layero, index) {
                    var body = layer.getChildFrame('body', index);
                    body.contents().find("#idleNumber").val(num);
                    body.contents().find("#orderDetailId").val(id);
                },
                area: ['300px', '180px'],
                content: ['idleNumber.html', 'yes'],
                btn: ["关闭", "保存"],
                yes: function (index, layero) {
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    var data = body.contents().find("#form").serializeObject();
                    data.type = 'xgxz';
                    return submit(data, 'xgxz');
                },
            });
        }
    }

    function submit(data, type) {
        $.ajax({
            type : 'put',
            url : '/stock/updateIdle',
            contentType: "application/json; charset=utf-8",
            data : data,
            success : function(data) {
                layer.msg(data.message, {shift: -1, time: 1000}, function(){
                    example.ajax.reload();
                });
            }
        });
    }

    layui.use(['layer', 'upload'], function () {
        var layer = layui.layer;
        var upload = layui.upload;

        //导入
        upload.render({ //允许上传的文件后缀
            elem: '#import'
            , url: '/stock/importData'
            , accept: 'file' //普通文件
            , exts: 'xls|xlsx' //只允许上传Excel
            , done: function (res) {
                example.ajax.reload();
                var failNum = res['failNum'];
                var fileName = res['fileName'];
                if (failNum > 0) {
                    layer.confirm("导入完成，成功" + res['successNum'] + "条，失败" + res['failNum'] + "条。是否要下载导入失败信息？", {
                        btn: ['确定', '以后再说']
                    }, function () {
                        $('#form').attr('action', '/files/downFailFile/' + fileName + '/stock').submit();
                        layer.close(1);
                    });
                } else {
                    layer.msg("导入完成，全部导入成功");
                }
            }
        });
    });

    function del(id) {
        layer.confirm('确定要删除吗？', {
            btn: ['确定', '取消']
        }, function () {
            $.ajax({
                type: 'delete',
                url: '/stock/' + id,
                success: function (data) {
                    example.ajax.reload();
                    layer.msg("删除成功");
                }
            });

            layer.close(1);
        });
    }

    $("#searchBt").click(function () {
        example.ajax.reload();
    });

    init();

    // 导出
    function exportData() {
        layer.confirm('请选择要导出的方式', {
            btn: ['导出数据', '导出空白模板'] //按钮
        }, function (index) {
            $('#form').attr('action', '/stock/export/1').submit();
            layer.close(index);
        }, function () {
            $('#form').attr('action', '/stock/export/0').submit();
        });
    }
</script>
